/******************************************************************************************
 * Data Structures in C++
 * ISBN: 7-302-33064-6 & 7-302-33065-3 & 7-302-29652-2 & 7-302-26883-3
 * Junhui DENG, deng@tsinghua.edu.cn
 * Computer Science & Technology, Tsinghua University
 * Copyright (c) 2006-2013. All rights reserved.
 ******************************************************************************************/

#pragma once

template <typename Tv, typename Te> template <typename PU> 
void Graph<Tv, Te>::pfs(int s, PU prioUpdater) { 
   reset(); int v = s; 
   do 
      if (UNDISCOVERED == status(v)) 
         PFS(v, prioUpdater); 
   while (s != (v = (++v % n))); 
}

template <typename Tv, typename Te> template <typename PU> 
void Graph<Tv, Te>::PFS(int s, PU prioUpdater) { 
   priority(s) = 0; status(s) = VISITED; parent(s) = -1; 
   while (1) { 
      for (int w = firstNbr(s); -1 < w; w = nextNbr(s, w)) 
         prioUpdater(this, s, w); 
      for (int shortest = INT_MAX, w = 0; w < n; w++)
         if (status(w) == UNDISCOVERED) 
            if (shortest > priority(w)) 
               { shortest = priority(w); s = w; } 
      if (VISITED == status(s)) break;
      status(s) = VISITED; status(parent(s), s) = TREE; 
   }
} 
